package edu.zqu.collegeField.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldApplicationStatus;
import edu.zqu.collegeField.project.mapper.FieldApplicationStatusMapper;
import edu.zqu.collegeField.project.service.FieldApplicationStatusService;
import edu.zqu.collegeField.project.vo.FieldApplicationStatusVo;
import edu.zqu.collegeField.project.vo.FieldVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 场地申请用途字典 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldApplicationStatusServiceImpl extends ServiceImpl<FieldApplicationStatusMapper, FieldApplicationStatus> implements FieldApplicationStatusService {
    @Autowired
    private FieldApplicationStatusMapper fieldApplicationStatusMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;

    @Override
    public List<FieldApplicationStatusVo> get(BigInteger collegeId, String name, String englishName) {//获取场地申请用途列表
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("englishName", Convert.toStr(englishName));
        map.put("name", name);
        List<FieldApplicationStatusVo> fieldApplicationStatusVoList = BeanMapper.mapList(fieldApplicationStatusMapper.get(map), FieldApplicationStatusVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldApplicationStatusVo fieldApplicationStatusVo : fieldApplicationStatusVoList) {
            fieldApplicationStatusVo.setCollegeName(collegeName);
        }
        return fieldApplicationStatusVoList;
    }

    @Override
    public FieldApplicationStatusVo getById(BigInteger id) {//获取场地申请用途
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldApplicationStatusVo fieldApplicationStatusVo = BeanMapper.map(fieldApplicationStatusMapper.getById(map), FieldApplicationStatusVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldApplicationStatusVo.getCollegeId());
        fieldApplicationStatusVo.setCollegeName(collegeName);
        return fieldApplicationStatusVo;
    }

    @Override
    public int add(FieldApplicationStatus fieldApplicationStatus) {//添加场地申请用途
        fieldApplicationStatus.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldApplicationStatusMapper.insert(fieldApplicationStatus);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldApplicationStatus fieldApplicationStatusUpdate) {//修改场地申请用途
        FieldApplicationStatus fieldApplicationStatus = fieldApplicationStatusMapper.selectById(fieldApplicationStatusUpdate.getId());
        if (StringUtils.isNull(fieldApplicationStatus)) {
            throw new NullPointerException("找不到该场地申请用途字典");
        }
        BeanMapper.copyPropertiesNotNull(fieldApplicationStatusUpdate, fieldApplicationStatus);
        try {
            int insert = fieldApplicationStatusMapper.insert(fieldApplicationStatus.setLastVersion(BigInteger.valueOf(fieldApplicationStatusUpdate.getId())));
            if (insert >= 1) {
                return delete(fieldApplicationStatusUpdate.getId(), fieldApplicationStatusUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(Integer id, BigInteger byWho) {//删除场地申请用途
        try {
            UpdateWrapper<FieldApplicationStatus> fieldApplicationStatusUpdateWrapper = new UpdateWrapper<FieldApplicationStatus>().eq("id", Convert.toStr(id));
            int update = fieldApplicationStatusMapper.update(new FieldApplicationStatus().setByWho(byWho), fieldApplicationStatusUpdateWrapper);
            if (update >= 1) {
                return fieldApplicationStatusMapper.delete(fieldApplicationStatusUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
